//
// Factory.cpp for plazza in /home/peelou/SVN/pizzaillo
// 
// Made by gery baudry
// Login   <baudry_g@epitech.net>
// 
// Started on  Mon Apr 15 17:50:35 2013 gery baudry
// Last update Wed Apr 17 16:41:08 2013 kevin lansel
//

#include	<sstream>
#include	<map>
#include	"Factory.hpp"

Factory::Factory()
{
}

Factory::~Factory()
{
}

APizza		*Factory::createRegina(TaillePizza taille, Stock *dec, int id)
{
  return (new Regina(taille, dec, id));
}

APizza		*Factory::createMargarita(TaillePizza taille, Stock *dec, int id)
{
  return (new Margarita(taille, dec, id));
}

APizza		*Factory::createAmericaine(TaillePizza taille, Stock *dec, int id)
{
  return (new Americaine(taille, dec, id));
}

APizza		*Factory::createFantasia(TaillePizza taille, Stock *dec, int id)
{
  return (new Fantasia(taille, dec, id));
}

std::string	Factory::pack(APizza *pizza)
{
  std::string				msg;
  std::map<TypePizza, std::string>	type;
  std::map<TaillePizza, std::string>	taille;
  std::stringstream			ss;
  std::string				tmp;

  type[PRegina] = "regina";
  type[PMargarita] = "margarita";
  type[PAmericaine] = "americaine";
  type[PFantasia] = "fantasia";
  taille[S] = "S";
  taille[M] = "M";
  taille[L] = "L";
  taille[XL] = "XL";
  taille[XXL] = "XXL";
  ss << pizza->getId();
  tmp = ss.str();
  msg += type[pizza->getType()] + " " + taille[pizza->getTaille()] + " " + tmp;
  return (msg);
}

APizza		*Factory::unpack(const std::string &msg, Stock *stock)
{
  std::map<std::string, TaillePizza>					taille;
  std::map<std::string, APizza*(Factory::*)(TaillePizza, Stock *, int)>	type;
  std::list<std::string>						spl;
  APizza								*pizza;
  Factory								fact;
  std::list<std::string>::iterator					it;
  std::stringstream							ss;
  int									id;

  spl = Lexer::split(msg, ' ');
  it = spl.begin();
  ++it;
  taille["S"] = S;
  taille["M"] = M;
  taille["L"] = L;
  taille["XL"] = XL;
  taille["XXL"] = XXL;
  type["regina"] = &Factory::createRegina;
  type["margarita"] = &Factory::createMargarita;
  type["americaine"] = &Factory::createAmericaine;
  type["fantasia"] = &Factory::createFantasia;
  APizza *(Factory::*tmp)(TaillePizza, Stock *, int) = type[spl.front()];
  ss.str(spl.back());
  ss >> id;
  pizza = (fact.*tmp)(taille[*it], stock, id);
  return (pizza);
}
